/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ecmascript/tests/test_helper.h"

#include "common_interfaces/thread/thread_holder.h"
#include "common_interfaces/thread/thread_holder_manager.h"
#include "common_interfaces/base_runtime.h"
#include "common_components/mutator/mutator.h"
#include "common_components/mutator/mutator_manager.h"

using namespace panda;
using namespace common;

namespace panda::test {
class ThreadHolderManagerTest : public BaseTestWithScope<false> {
};

size_t callCount_;
void CallBackTest(void *root)
{
    callCount_++;
}

HWTEST_F_L0(ThreadHolderManagerTest, ThreadHolderManagerTest0) {
    ThreadHolder *curHolder = ThreadHolder::CreateAndRegisterNewThreadHolder(nullptr);
    CommonRootVisitor visitor = &CallBackTest;

    callCount_ = 0;
    ThreadHolderManager thManager;
    thManager.BindMutator(curHolder);
    thManager.IterateAll(visitor);

    EXPECT_EQ(callCount_, 2);
}

HWTEST_F_L0(ThreadHolderManagerTest, ThreadHolderManagerTest1) {
    ThreadHolder *curHolder = ThreadHolder::CreateAndRegisterNewThreadHolder(nullptr);
    Mutator *mutator = static_cast<Mutator *>(curHolder->GetMutator());
    
    auto& mutator_manager = MutatorManager::Instance();
    auto& list = mutator_manager.allMutatorList_;
    list.clear();

    ThreadHolderManager thManager;
    thManager.UnregisterThreadHolder(curHolder);
    EXPECT_EQ(mutator->PopRawObject(), nullptr);
}

} // namespace panda::test